/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.foment.data;

import java.io.Serializable;
import java.util.HashSet;


/**
 * klasa User
 * sadrzi username i password koji ce se slati bazi.
 * iz baze,nakon validacije, imacemo fullname
 * @author dusan
 */
public class User implements Serializable
{
    public User(int id, String name, String u, String p, boolean privAll, boolean privReadOnly) {

        username = u;
        password = p;
        fullName = name;
        _id = id;
        _privAllAccess = privAll;
        _privReadOnly  = privReadOnly;
    }

    public User() {

        username = "";
        password = "";
        fullName = "";
        _privAllAccess = false;
        _privReadOnly  = false;
        _id = -1;
    }

    public String getUsername() {

        return username;
    }

    public String getPassword() {

        return password;
    }

    public String getFullName() {

        return fullName;
    }

    public boolean getPrivAllAccess() {
        return _privAllAccess;
    }

    public boolean getPrivReadOnly() {
        return _privReadOnly;
    }

    public int getID() {

        return _id;
    }

    public void setID(int id) {
        _id = id;
    }

    public void setUserName(String u) {
        username = u;
    }

    public void setPassword(String p) {
        username = p;
    }

    public void setFullName(String f) {
        fullName = f;
    }

    public void setPrivAllAccess(boolean p) {
        _privAllAccess = p;
    }

    public void setPrivReadOnly(boolean p) {
        _privReadOnly = p;
    }

    private int _id;
    private boolean _privAllAccess;
    private boolean _privReadOnly;
    private String fullName;
    private String username;
    private String password;


    @Override
    public String toString()
    {
        return username + " " + fullName;
    }


  /**
    * Interface to be implemented by objects that need to be notified
    * about user logged in
    * @author Jaksa
  */
  public interface UserListener {

    /**
     * This method is used to notify the listener that user is logged in
     */
    public void userLoggedIn();
  }


  private static HashSet < UserListener > _listeners = new HashSet();

  /**
   * Registers a listener with a user
   * @return true if added. false if the specified listener is already listening
   * for the events of the specified table
   * @param userListener Listener object
   */
  public boolean addListener(UserListener userListener) {
    if (_listeners.contains(userListener)) return false;
    _listeners.add(userListener);
    return true;
  }

  /**
   * Removes a listener from the user
   * @return true if success. false if the specified listener was not listening
   * to the specified table
   * @param userListener The listener to remove
   */
  public boolean removeListener(UserListener userListener) {
    if (!_listeners.contains(userListener)) return false;
    _listeners.remove(userListener);
    return true;
  }


  /**
   * Notifies the listeners that user is logged in.
   */
  public void notifyUserLoggedIn() {
    for (UserListener listener: _listeners) {
      listener.userLoggedIn();
    }
  }


}
